package cn.micro.system.manage.controller;


import cn.micro.system.manage.common.result.Result;
import cn.micro.system.manage.common.security.utils.SecurityUtil;
import cn.micro.system.manage.entity.AssetOdsCustomizedProductsApply;
import cn.micro.system.manage.entity.dto.ApprovedDTO;
import cn.micro.system.manage.entity.dto.CustomizedProductsApplySearch;
import cn.micro.system.manage.service.AssetOdsCustomizedProductsApplyService;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 * 定制产品申请表 前端控制器
 * </p>
 *
 * @author wr
 * @since 2024-01-09
 */
@RestController
@RequestMapping("/manage/assetOdsCustomizedProductsApply")
@RequiredArgsConstructor
@Slf4j
public class AssetOdsCustomizedProductsApplyController {

    private final AssetOdsCustomizedProductsApplyService productsApplyService;

    @GetMapping("/{page}/{size}")
    public Object list(@PathVariable int page, @PathVariable int size, CustomizedProductsApplySearch search){
        return Result.success(new PageInfo<>(productsApplyService.listByPage(page,size,search)));
    }

    @PostMapping("apply")
    public Object apply(@RequestBody AssetOdsCustomizedProductsApply customizedProductsApply){
        if (!productsApplyService.apply(customizedProductsApply)){
            return Result.fail();
        }
        return Result.success();
    }


    @PostMapping("approve")
    public Object approve(@RequestBody ApprovedDTO dto){
        AssetOdsCustomizedProductsApply productsApply = new AssetOdsCustomizedProductsApply();
        productsApply.setId(dto.getId());
        productsApply.setApplyStatus(dto.getStatus());
        productsApply.setApprovedDesc(dto.getApproveDesc());
        productsApply.setActualFees(dto.getActualFees());
        productsApply.setUpdateTime(LocalDateTime.now());
        productsApply.setUpdateBy(SecurityUtil.getUserId());
        if (!productsApplyService.updateById(productsApply)){
            return Result.fail();
        }
        return Result.success();
    }

    @DeleteMapping("delete/{id}")
    public Object delete(@PathVariable String id){
        if (!productsApplyService.removeById(id)){
            return Result.fail();
        }
        return Result.success();
    }



}
